<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>礼品管理</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">礼品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="lipin" placeholder="请输入礼品名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">提供商家</label>
                            <div class="layui-input-inline">
                                <input type="text" name="info" placeholder="请输入商家昵称或手机号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">领取方法</label>
                            <div class="layui-input-inline">
                                <select name="mode" lay-verify="">
                                    <option value="">请选择领取方法</option>
                                    <option value="0">到店免费领取</option>
                                    <option value="1">到店消费领取</option>
                                    <option value="2">线上充值领取</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status" lay-verify="">
                                    <option value="">请选择</option>
                                    <option value="1">未生成核销码</option>
                                    <option value="2">已生成核销码</option>
                                    <option value="3">核销码全部失效</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加礼品</button>
                    </div>
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="img">
                        <img src="/uploads/{{d.img}}" alt="" style="width: 60px;height: 60px;">
                    </script>
                    <script type="text/html" id="mode">
                        {{# if (d.mode == 0){ }}
                        到店免费领取
                        {{# }else if(d.mode == 1){ }}
                        到店消费领取
                        {{# }else if(d.mode == 2){ }}
                        线上充值领取
                        {{# }else{ }}
                        线上免费领取
                        {{# } }}
                    </script>
                    <script type="text/html" id="money">
                        {{# if (d.mode == 2){ }}
                        充值对等金额
                        {{# }else{ }}
                        {{ returnFloat(d.money) }} ¥
                        {{# } }}
                    </script>
                    <script type="text/html" id="is_zu">
                        {{# if (d.is_zu == 1){ }}
                        <input type="checkbox" data-id="{{d.id}}" value="1" lay-filter="zu" lay-skin="switch" lay-text="ON|OFF" checked>
                        {{# }else{ }}
                        <input type="checkbox" data-id="{{d.id}}" value="1" lay-filter="zu" lay-skin="switch" lay-text="ON|OFF">
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        {{# if (d.mode == 0 || d.mode == 1){  }}
                        {{# if (d.is == 0){ }}
                        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="create"><i class="layui-icon layui-icon-tabs"></i>生成核销码</a>
                        {{# }else{ }}
                        <a class="layui-btn layui-btn-xs" lay-event="jilu"><i class="layui-icon layui-icon-tabs"></i>核销记录</a>
                        {{# } }}
                        {{# } }}
                        {{# if (d.is == 0){  }}
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        {{# } }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="ling">领取记录</a>
                    </script>
                </div>
            </div>
        </div>
    </fieldset>

</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table;

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('Gift/index')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        form.on('switch(zu)', function(data){
            var index = layer.load();
            var id = $(data.elem).data('id');
            var value = 0;
            if (data.elem.checked){
                value = 1;
            }else{
                value = 0;
            }
            $.post("{:url('Gift/gift_zu')}", {id:id,value:value}, function(e){
                layer.close(index);
                if (e.code == 200){
                    layer.msg(e.data);
                }else{
                    layer.msg(e.msg);
                    setTimeout(function (){
                        table.reload('demo');
                    }, 500);
                }
            }, 'json');
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: '/admin/Gift/index',
            method: 'post',
            cols: [[
                {field: 'lipin', title: '礼品名称'},
                {field: 'uid', title: '提供礼品商家'},
                {field: 'img', title: '礼品封面', templet: "#img"},
                {field: 'num', title: '礼品数量'},
                {field: 'stage', title: '期数'},
                {field: 'mode', title: '领取方法', templet: "#mode"},
                {field: 'money', title: '任务金额', templet: "#money"},
                {field: 'address', title: '商家地址'},
                {field: 'is_zu', title: '是否允许组合', width: 130, templet: "#is_zu"},
                {fixed: 'right', title: '操作', width: 250, align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        form.on('switch(filter)', function(data){
            console.log(data.elem); //得到checkbox原始DOM对象
            console.log(data.elem.checked); //开关是否开启，true或者false
            console.log(data.value); //开关value值，也可以通过data.elem.value得到
            console.log(data.othis); //得到美化后的DOM对象
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'edit'){ //编辑
                layer.open({
                    type: 2
                    ,title: '礼品编辑'
                    ,content: '/admin/Gift/gift_operate/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            }else if(layEvent === 'del'){
                layer.confirm('确定删除吗?', {
                    icon: 3,
                    title:'提示',
                    offset: ['25%', '35%']
                }, function(){
                    $.post("{:url('Gift/gift_del')}", {id:id}, function(e){
                        if (e == 1){
                            layer.msg('删除成功', {offset: ['30%', '40%']});
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else if (e == 2){
                            layer.msg('大礼包存在该礼品不能删除!', {offset: ['30%', '40%']});
                        }else{
                            layer.msg('删除失败', {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            }else if(layEvent === 'create'){
                layer.confirm('确定生成核销码吗? 生成核销码之后不能再次编辑!', {
                    title:'提示',
                    offset: ['25%', '35%'],
                    area: '300px'
                }, function(){
                    var index = layer.load(1, {offset: ['30%', '42%']});
                    $.post("{:url('Gift/create')}", {id:id}, function(e){
                        layer.close(index);
                        if (e.info == 1){
                            layer.msg('生成成功', {offset: ['30%', '40%']});
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg(e.msg, {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            }else if(layEvent === 'jilu'){
                layer.open({
                    type: 2
                    ,title: '核销记录'
                    ,content: '/admin/Cash/he/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: '关闭'
                    ,btnAlign: 'c'
                });
            }else if(layEvent === 'ling'){
                layer.open({
                    type: 2
                    ,title: '领取记录'
                    ,content: '/admin/Gift/ling/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: '关闭'
                    ,btnAlign: 'c'
                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加礼品'
                    ,content: "{:url('Gift/gift_operate')}"
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    //补零
    function returnFloat(value){
        value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split(".");
        if(xsd.length == 1){
            value = value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value = value.toString()+"0";
            }
            return value;
        }
    }
</script>
</body>
</html>
